package org.example.study2;

public class ThreadStopDemo2 {
    static class Worker implements Runnable {
        @Override
        public void run() {
            while (!Thread.currentThread().isInterrupted()) {
                System.out.println(Thread.currentThread().getName()
                        + ": 转账处理中...");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    System.out.println("收到中断信号！终止交易！");
                    break; // 关键：必须跳出循环
                }
            }
            System.out.println(Thread.currentThread().getName()
                    + ": 转账已终止");
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread worker = new Thread(new Worker(), "张三");
        System.out.println("主线程: 张三转账");
        worker.start();

        Thread.sleep(5000);
        System.out.println("主线程: 紧急通知！对方是骗子！");
        worker.interrupt(); // 发送中断请求
    }
}